﻿using System;
using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using Caliburn.Micro;
using JY.Common.Enums;
using JY.Wpf.Core.Enums;
using JY.Wpf.Core.Events;
using JY.Wpf.Core.Models;
using JY.Wpf.Core.Tools;

namespace JY.Wpf.Core.ViewModelBases
{
    /// <summary>
    /// 窗体页面ViewModel
    /// </summary>
    public class WindowViewModelBase : ViewModelBase, IHandle<BusyIndicatorEvent>, IHandle<WarningMessageEvent>
    {
        #region # 变量
        
        #endregion

        #region # 构造器
        /// <summary>
        /// 构造器
        /// </summary>
        public WindowViewModelBase()
        {
            this.EventAggregator.Subscribe(this, true);
            this.IsOpen = true;
            this.Width = 1200;
            this.Height = 600;
            this.WindowState = WindowState.Normal;
        }

        #endregion

        #region # 属性
        #region 是否打开——bool IsOpen
        private bool _isOpen;
        /// <summary>
        /// 是否打开
        /// </summary>
        public bool IsOpen
        {
            get { return this._isOpen; }
            set
            {
                this._isOpen = value;
                if (!this._isOpen)
                {
                    this.Deactivate();
                }
                base.NotifyOfPropertyChange();
            }
        }
        #endregion
        #region 是否Dialog窗体——bool IsDialog
        private bool _isDialog;
        /// <summary>
        /// 是否Dialog窗体
        /// </summary>
        public bool IsDialog
        {
            get { return this._isDialog; }
            set { this._isDialog = value; base.NotifyOfPropertyChange(); }
        }
        #endregion

        #region 宽度——double Width
        private double _width;
        /// <summary>
        /// 宽度
        /// </summary>
        public double Width
        {
            get { return this._width; }
            set { this._width = value; base.NotifyOfPropertyChange(); }
        }

        #endregion

        #region 高度——double Height
        private double _height;
        /// <summary>
        /// 高度
        /// </summary>
        public double Height
        {
            get { return this._height; }
            set { this._height = value; base.NotifyOfPropertyChange(); }
        }
        #endregion

        #region 窗体状态——WindowState WindowState
        private WindowState _windowState;
        /// <summary>
        /// 窗体状态
        /// </summary>
        public WindowState WindowState
        {
            get { return this._windowState; }
            set { this._windowState = value; base.NotifyOfPropertyChange(); }
        }
        #endregion

        #region # 警告消息——WarningMessage WarningMessage
        private WarningMessage _warningMessage;
        /// <summary>
        /// 警告消息
        /// </summary>
        public WarningMessage WarningMessage
        {
            get { return this._warningMessage; }
            set { this._warningMessage = value; base.NotifyOfPropertyChange(); }
        }
        #endregion
        #endregion

        #region # Command 方法

        #region # 关闭窗体中——void ChildWindowClosing(CancelEventArgs args)
        /// <summary>
        /// 关闭窗体中
        /// </summary>
        /// <param name="args">取消事件参数</param>
        public virtual void WindowClosing(CancelEventArgs args)
        {
            args.Cancel = false;
        }
        #endregion

        #region # 已关闭窗体——void WindowClosed(EventArgs args)
        /// <summary>
        /// 已关闭窗体
        /// </summary>
        /// <param name="args">事件参数</param>
        public virtual void WindowClosed(EventArgs args)
        {
            this.EventAggregator.Unsubscribe(this);
            this.IsOpen = false;
            this.Deactivate();
        }
        #endregion

        #region # 关闭窗体——void Close()
        /// <summary>
        /// 关闭窗体
        /// </summary>
        public void Close()
        {
            var window = (this.GetView() as Window) ?? VisualHelper.FindParent<Window>(this.GetView() as FrameworkElement);
            window?.Close();
        }
        #endregion 
        #endregion

        #region # Events 方法

        #region # 显示遮罩事件方法——void HandleAsync(BusyIndicatorEvent busyIndicatorEvent)
        /// <summary>
        /// 显示遮罩事件方法
        /// </summary>
        public virtual Task HandleAsync(BusyIndicatorEvent eventData, CancellationToken cancellationToken)
        {
            Application.Current.BeginInvoke(() =>
            {
                if (eventData.IsBusy)
                {
                    this.Busy(eventData.BusyContent?.ToString());
                }
                else
                {
                    this.Idle();
                }
            });
            return Task.CompletedTask;
        }
        #endregion

        #region # 警告消息事件方法——void HandleAsync(WarningMessageEvent eventData)
        /// <summary>
        /// 警告消息事件方法
        /// </summary>
        public virtual Task HandleAsync(WarningMessageEvent eventData, CancellationToken cancellationToken)
        {
            this.ShowWarningMessage(eventData.Message, eventData.MessageType);
            return Task.CompletedTask;
        }
        #endregion
        #endregion

        #region # 方法

        #region # 显示警告消息——void ShowWarningMessage(string message...
        /// <summary>
        /// 显示警告消息
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="messageType">消息类型</param>
        protected void ShowWarningMessage(string message, MessageType messageType = MessageType.Error)
        {
            Application.Current.BeginInvoke(() =>
            {
                this.WarningMessage = new WarningMessage(message, messageType);
            });
        } 
        #endregion

        #region # 页面失活——void Deactivate()
        /// <summary>
        /// 页面失活
        /// </summary>
        public override void Deactivate()
        {
            this.EventAggregator.Unsubscribe(this);
            base.Deactivate();
        }
        #endregion
        #endregion
    }
}
